﻿using Advantech.Motion;
using APS168_W32;
using CommonTool;
using Motions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tool;

namespace AutoMachine
{
    public partial class Frm_Axis : Form
    {
        Tool.AutoResize FormResize = new Tool.AutoResize();//窗体缩放实例化

        #region 窗体实例化
        /// <summary>
        /// 窗体对象实例化
        /// </summary>
        private static Frm_Axis _instance;
        internal static Frm_Axis Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new Frm_Axis();
                return _instance;
            }
        }
        #endregion


        public Frm_Axis()
        {
            InitializeComponent();
            FormResize.SetAutoResize(this);//窗体缩放实例化
        }

        private void Frm_Axis_Load_1(object sender, EventArgs e)
        {
            //如果轴号从0开始 就直接用这段程序
            //如果轴号从1开始 item.Index 需要减1；
            Resize += new EventHandler(Form1_Resize);
            X = Width;
            Y = Height;

            setTag(this);
            Form1_Resize(new object(), new EventArgs());


            foreach (SystemCfg.AxisNameInfo item in SystemCfg.AxisName.Values)
            {
                this.dgv_AxisWrite.Rows.Add(item.Index.ToString());
                this.dgv_AxisWrite.Rows[item.Index].Cells[0].Value = item.Index;//编号
                this.dgv_AxisWrite.Rows[item.Index].Cells[1].Value = item.Name;//名称
                this.dgv_AxisWrite.Rows[item.Index].Cells[5].Value = 10;//默认轴速度
                this.dgv_AxisWrite.Rows[item.Index].Cells[6].Value = 5;//相对距离
                this.dgv_AxisWrite.Rows[item.Index].Cells[7].Value = 0;//绝对位置
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (MotionMgr.GetInstance().Card.CardIniOK)
            {
                #region  状态更新

                for (int i = 0; i < dgv_AxisWrite.Rows.Count; i++)
                {
                    int AxisNo = Convert.ToInt16(this.dgv_AxisWrite.Rows[i].Cells[0].Value.ToString());   //第0列代表的是轴编号

                    // i是行号 1是列号
                    //Sever On
                    if (MotionMgr.GetInstance().Card.GetAxisSeverOn(AxisNo) == true)//输入Sever ON 
                    {
                        Tool.CtrDisplay.ShowText(this.dgv_AxisWrite, i, 2, "ON", Color.Green, Color.Empty);
                    }
                    else
                    {
                        Tool.CtrDisplay.ShowText(this.dgv_AxisWrite, i, 2, "OFF", Color.White, Color.Empty);
                    }
                    //原点信号
                    if (MotionMgr.GetInstance().Card.GetAxisHome(AxisNo) == true)//输入原点信号
                    {
                        Tool.CtrDisplay.ShowText(this.dgv_AxisWrite, i, 17, "", Color.Green, Color.Empty);
                    }
                    else
                    {
                        Tool.CtrDisplay.ShowText(this.dgv_AxisWrite, i, 17, "", Color.White, Color.Empty);
                    }
                    //正限位
                    if (MotionMgr.GetInstance().Card.GetPosLimit(AxisNo) == true)//输入正限位
                    {
                        Tool.CtrDisplay.ShowText(this.dgv_AxisWrite, i, 16, "", Color.Green, Color.Empty);
                    }
                    else
                    {
                        Tool.CtrDisplay.ShowText(this.dgv_AxisWrite, i, 16, "", Color.White, Color.Empty);
                    }
                    //负限位
                    if (MotionMgr.GetInstance().Card.GetNegLimit(AxisNo) == true)//输入负限位
                    {
                        Tool.CtrDisplay.ShowText(this.dgv_AxisWrite, i, 15, "", Color.Green, Color.Empty);
                    }
                    else
                    {
                        Tool.CtrDisplay.ShowText(this.dgv_AxisWrite, i, 15, "", Color.White, Color.Empty);
                    }
                    //报警信号
                    if (MotionMgr.GetInstance().Card.GetAxisAlarm(AxisNo) == true)//输入报警信号
                    {
                        Tool.CtrDisplay.ShowText(this.dgv_AxisWrite, i, 18, "", Color.Green, Color.Empty);
                    }
                    else
                    {
                        Tool.CtrDisplay.ShowText(this.dgv_AxisWrite, i, 18, "", Color.White, Color.Empty);
                    }

                    // Ready信号
                    if (MotionMgr.GetInstance().Card.GetAxisReady(AxisNo) == true)//输入Ready信号
                    {
                        Tool.CtrDisplay.ShowText(this.dgv_AxisWrite, i, 19, "", Color.Green, Color.Empty);
                    }
                    else
                    {
                        Tool.CtrDisplay.ShowText(this.dgv_AxisWrite, i, 19, "", Color.White, Color.Empty);
                    }

                    ////Busy信号
                    //if (MotionMgr.GetInstance().Card.GetAxisBusy(AxisNo) == true)//输入Busy信号
                    //{
                    //    Tool.CtrDisplay.ShowText(this.dgv_AxisWrite, i, 20, "T", Color.Green, Color.Empty);
                    //}
                    //else
                    //{
                    //    Tool.CtrDisplay.ShowText(this.dgv_AxisWrite, i, 20, "F", Color.White, Color.Empty);
                    //}

                    // 完成信号
                    //if (MotionMgr.GetInstance().Card.GetAxisDone(AxisNo) == true)//输入定位完成信号
                    //{
                    //    Tool.CtrDisplay.ShowText(this.dgv_AxisWrite, i, 22, "T", Color.Green, Color.Empty);
                    //}
                    //else
                    //{
                    //    Tool.CtrDisplay.ShowText(this.dgv_AxisWrite, i, 22, "F", Color.White, Color.Empty);
                    //}



                }

                #endregion
            }

        }


        private void timer2_Tick(object sender, EventArgs e)
        {
            if (MotionMgr.GetInstance().Card.CardIniOK)
            {
                //当前坐标位置
                for (int j = 0; j < dgv_AxisWrite.Rows.Count; j++)
                {
                    int nAxisNo = Convert.ToInt16(this.dgv_AxisWrite.Rows[j].Cells[0].Value.ToString());   //第0列代表的是轴编号
                                                                                                           // 当前位置
                    Tool.CtrDisplay.ShowText(this.dgv_AxisWrite, j, 4, MotionMgr.GetInstance().Card.GetAxisActPos(nAxisNo).ToString());//轴当前位置
                }
            }
          
        }

        private void dgv_AxisWrite_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {
            if (Device.StateMode == Device.STATEMode.RUN)
            {
                
                MessageBox.Show("设备正在自动运行,请勿点击");
                return;
            }

            //if (e.ColumnIndex == 9 || e.ColumnIndex == 10)//防止误点
            //{
            //    //dgv_AxisWrite_CellMouseDown(null, null);
            //    return;

            //}
            // dgv_AxisWrite.ClearSelection();  //清除默认选中行
            if (this.dgv_AxisWrite.SelectedCells.Count > 0)
            {
                int row = dgv_AxisWrite.SelectedCells[0].RowIndex;//行号 选择的行号

                int col = dgv_AxisWrite.SelectedCells[0].ColumnIndex;//列号  旋转的列号

                int AxisNo = Convert.ToInt16(this.dgv_AxisWrite.Rows[row].Cells[0].Value.ToString()); //选择行号的第一列 代表轴号


                switch (col)
                {
                    case 2://使能按钮

                        if (MotionMgr.GetInstance().Card.GetAxisSeverOn(AxisNo) == true)
                        {
                            MotionMgr.GetInstance().Card.ServoOff(AxisNo);//断使能
                        }
                        else
                        {
                            MotionMgr.GetInstance().Card.ServoOn(AxisNo);//上使能
                        }

                        break;
                    case 3://回原按钮               
                        MotionMgr.GetInstance().Card.HomeMove(AxisNo, SystemCfg.AxisCfg[AxisNo].HomeDir, SystemCfg.AxisCfg[AxisNo].HomeMode);//轴回原
                        MotionMgr.GetInstance().WaitHome(AxisNo);
                        break;

                    case 10://相对+
                        MotionMgr.GetInstance().Card.RelMove(AxisNo, Convert.ToDouble(dgv_AxisWrite.Rows[row].Cells[6].Value.ToString()), Convert.ToDouble(dgv_AxisWrite.Rows[row].Cells[5].Value.ToString()));
                        break;
                    case 11://相对-
                        MotionMgr.GetInstance().Card.RelMove(AxisNo, Convert.ToDouble(dgv_AxisWrite.Rows[row].Cells[6].Value.ToString()) * -1, Convert.ToDouble(dgv_AxisWrite.Rows[row].Cells[5].Value.ToString()));
                        break;
                    case 12://绝对定位
                        MotionMgr.GetInstance().Card.AbsMove(AxisNo, Convert.ToDouble(dgv_AxisWrite.Rows[row].Cells[7].Value.ToString()), Convert.ToDouble(dgv_AxisWrite.Rows[row].Cells[5].Value.ToString()));
                        break;
                    case 13://轴停
                        MotionMgr.GetInstance().Card.EmgAxis(AxisNo);//轴紧急停止
                        break;
                    case 14://报警清除
                        MotionMgr.GetInstance().Card.ClearAxisAlarm(AxisNo);//轴报警清除
                        break;
                }
                string selectValue = dgv_AxisWrite.CurrentCell.EditedFormattedValue.ToString();

                if (selectValue == "True")
                {
                    if (col == 8)//Jog+
                    {
                        MotionMgr.GetInstance().Card.JogMove(AxisNo, Convert.ToDouble(dgv_AxisWrite.Rows[row].Cells[5].Value.ToString()), true);//JOG+               
                    }
                    if (col == 9)//Jog-
                    {
                        MotionMgr.GetInstance().Card.JogMove(AxisNo, Convert.ToDouble(dgv_AxisWrite.Rows[row].Cells[5].Value.ToString()), false);//JOG-
                    }
                }
                if (selectValue == "False")
                {
                    if (col == 8 || col == 9)
                    {

                        MotionMgr.GetInstance().Card.StopAxis(AxisNo);//轴停
                    }
                }



            }
        }

        private void dgv_AxisWrite_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            int a = e.RowIndex;
            var row = dgv_AxisWrite.Rows[a];
            if (row == dgv_AxisWrite.CurrentRow)
            {
                row.DefaultCellStyle.BackColor = Color.Yellow;
            }
            else
            {
                row.DefaultCellStyle.BackColor = Color.White;
            }
        }

        #region 窗体缩放
        private float X;
        private float Y;

        private void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size;
                if (con.Controls.Count > 0)
                    setTag(con);
            }
        }
        private void setControls(float newx, float newy, Control cons)
        {
            foreach (Control con in cons.Controls)
            {

                string[] mytag = con.Tag.ToString().Split(new char[] { ':' });
                float a = Convert.ToSingle(mytag[0]) * newx;
                con.Width = (int)a;
                a = Convert.ToSingle(mytag[1]) * newy;
                con.Height = (int)(a);
                a = Convert.ToSingle(mytag[2]) * newx;
                con.Left = (int)(a);
                a = Convert.ToSingle(mytag[3]) * newy;
                con.Top = (int)(a);
                Single currentSize = Convert.ToSingle(mytag[4]) * Math.Min(newx, newy);
                if (currentSize == 0)
                {
                    currentSize = 1;
                }
                con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                if (con.Controls.Count > 0)
                {
                    setControls(newx, newy, con);
                }
            }

        }

        void Form1_Resize(object sender, EventArgs e)
        {
            float newx = (Width) / X;
            float newy = Height / Y;
            setControls(newx, newy, this);
            //Text = Width.ToString() + " " + Height.ToString();

        }
        #endregion
    }
}
